package com.qcby.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qcby.dao.StudentDao;
import com.qcby.entity.Student;
import com.qcby.entity.vo.StudentVo;
import com.qcby.service.StudentService;
import com.qcby.util.ResultObject;
import com.qcby.util.SystemDescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author haozeWang
 * @create 2021-08-23 17:46
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    /*查所有*/
    @Override
    public ResultObject selectAll(StudentVo studentVo) {
        Page<Object> page = PageHelper.startPage(studentVo.getPage(), studentVo.getLimit());

        if(studentVo.getName()!=null&&!studentVo.getName().equals("")){
            studentVo.setName("%"+studentVo.getName()+"%");
        }else {
            studentVo.setName(null);
        }
        if(studentVo.getSnumber()!=null&&!studentVo.getSnumber().equals("")){
            studentVo.setSnumber("%"+studentVo.getSnumber()+"%");
        }else {
            studentVo.setSnumber(null);
        }
        if(studentVo.getClass_id()!=null&&!studentVo.getClass_id().equals("")){
        }else {
            studentVo.setClass_id(null);
        }

        List<StudentVo> students = studentDao.selectAll(studentVo);
        ResultObject resultObject = new ResultObject("",0,page.getTotal(),students);
        System.out.println(page.getResult());
        return resultObject;
    }
    /*删除单个byid*/
    @Override
    public ResultObject delete(Integer id) {
        Student student = new Student();
        student.setId(id);
        student.setFlag(SystemDescription.CODECURDFAIL);
        int i = studentDao.updateByPrimaryKeySelective(student);
        String msg=i==0?SystemDescription.DELETEFAIL:SystemDescription.DELETEOK;
        ResultObject resultObject = new ResultObject(i, msg);
        return resultObject;
    }
    /*添加学生*/
    @Override
    public ResultObject add(Student student) {
        int i = studentDao.insertSelective(student);
        String msg = i== SystemDescription.CODEFAIL ?SystemDescription.ADDFAIL:SystemDescription.ADDOK;
        ResultObject resultObject = new ResultObject(i, msg);
        return resultObject;
    }
    /*修改学生*/
    @Override
    public ResultObject update(Student student) {
        int i = studentDao.updateByPrimaryKeySelective(student);
        String msg = i== SystemDescription.CODEFAIL ?SystemDescription.UPDATEFAIL:SystemDescription.UPDATEOK;
        ResultObject resultObject = new ResultObject(i, msg);
        return resultObject;
    }

    /*批量删除学生*/
    @Override
    public ResultObject deleteDisp(ArrayList<Integer> ids) {


        int flag=SystemDescription.CODECURDOK;
        for (Integer id:ids) {
            ResultObject deleteitem = delete(id);
             if(deleteitem.getCode()==SystemDescription.CODECURDFAIL){
                 flag=SystemDescription.CODECURDFAIL;
             }
        }
        String msg=flag==0?SystemDescription.DELETEDISPFAIL:SystemDescription.DELETEDISPOK;
        ResultObject resultObject = new ResultObject(flag,msg);
        return resultObject;
    }
}
